package com.briup.common.sms;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

import java.util.Map;

/**
 * @Description 短信验证码工具类-aliyun
 * @Author wood
 */
public class AliyunSmsUtil {

    private static final String accessKeyId = "LTAI4GFmXGwtxxxxxxxxxxxxx";
    private static final String secret = "TmdC5QxBJsIFYR32cxxxxxxxxxxxxx";
    private static final String SignName = "woodcool技术组";
    private static final String TemplateCode = "SMS_191210179";

    /**
     *
     * @param telephone  电话号码
     * @param map        阿里云中设置好的短信模板，短信模板中需要传参，以key-value的方式给出
     * @return
     */
    public static boolean sendSms(String telephone, Map<String,String> map){

        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, secret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");

        request.putQueryParameter("PhoneNumbers", telephone);//手机号
        request.putQueryParameter("SignName", SignName);//签名
        request.putQueryParameter("TemplateCode", TemplateCode); //模板code

        //map转为json字符串后当做参数传入
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(map));
        CommonResponse response = null;
        try {
            response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ClientException e) {
            e.printStackTrace();
        }

        return response.getHttpResponse().isSuccess();

//        return true;
    }

}
